// 众筹首页JS

$(function(){
    var url = new_url+'shop/crowdfunding/getCrowdItemList';
    var url_1 = new_url+'shop/crowdfunding/getFinishCrowdItemList';
    // 众筹项目列表
    $.ajax({
        type:'post',
        url:url,
        dataType:'json',
        // data:data,
        success:function(e){
            if(e.code == '0'){
                // 隐藏正在加载中动画
                $('.crowd_box_bg, .crowd_serve_bg').css('display','none');
                // 显示
                $('.crowd_serve').css('display','block');
                // 时间格式化
                var nowTimeStamp = Math.round(new Date().getTime()/1000);
                var crowdFundingTpl = $("#crowdFundingTpl").html();
                if(e.data.length!='0'){
                    for(var i=0;i<e.data.length;i++){ 
                        // 剩余时间（天）
                        var timeRemaining = e.data[i].timeRemaining;
                        if(timeRemaining != '0'){
                            var timeRemainOne = formatDate(timeRemaining);
                            var remainingTime = getEndTime(timeRemainOne);
                        }else{
                            var remainingTime = '∞';
                        }
                        // 小数转化成百分数
                        var raiseProportion = e.data[i].raiseProportion;
                        var raisePercent = toPercent(raiseProportion);      //筹集比例
                        // 进度条显示比例
                        if(raiseProportion>='1'){
                            var progressBarPercent='100%';
                        }else{
                            var progressBarPercent=toPercent(raiseProportion);
                        }
                        // 项目状态 2.众筹中 3.已完成 4已结算 5.失败
                        var stateDect = '';
                        // console.log('crowdBox'+e.data[i].itemCommonId);
                        var k = '#crowdBox'+e.data[i].itemCommonId;
                        // 项目状态 2.众筹中 3.已完成 4已结算 5.失败
                        // 按钮显示状态显示
                        var str_1 = '';
                        var str_2 = '';
                        str_1 += '<a class="view_crowd" href="crowdfunding_detail.html?Id='+e.data[i].itemCommonId+'">支持项目</a>';
                        str_2 += '<a class="view_crowded" href="crowdfunding_detail.html?Id='+e.data[i].itemCommonId+'">支持项目</a>';
                        if(e.data[i].itemState == '2'){
                            stateDect = '众筹中';
                        }
                        if(e.data[i].itemState == '3'){
                            stateDect = '已完成';
                        }
                        if(e.data[i].itemState == '5'){
                            stateDect = '失败';
                        }
                        var crowdFundingListCont=template( 
                            crowdFundingTpl, { 
                                crowdFundingList: [{ 
                                    itemCommonId:e.data[i].itemCommonId,           //众筹项目Id
                                    itemName:e.data[i].itemName,                   //众筹项目名称
                                    itemSummary:e.data[i].itemSummary,             //众筹项目简介
                                    exhibitionImage:e.data[i].exhibitionImage,     //众筹项目封面图片
                                    raisedAmount:e.data[i].raisedAmount,           //已筹资金
                                    timeRemaining:remainingTime,         //剩余时间
                                    raiseProportion:raisePercent,        //筹集比例
                                    progressBarPercent:progressBarPercent,      // 进度条百分比  最高为100%
                                    itemState:e.data[i].itemState,       //项目状态 2.众筹中 3.已完成 4.失败
                                    stateDect:stateDect
                                }] 
                            } 
                        ); 
                        $('.crowd_list').append(crowdFundingListCont); 
                        if(e.data[i].itemState == '2'){
                            // '众筹中';
                            $(k).find(".list_right").append(str_1);
                        }
                        if(e.data[i].itemState == '3'){
                            // '已完成';
                            $(k).find(".list_right").append(str_2);
                            $(k).find(".crowd_status").css('color','#2fd6a2');
                            $(k).find(".crowd_jd").addClass('crowd_jd_green')
                            
                        }
                        if(e.data[i].itemState == '5'){
                            // '失败';
                            $(k).find(".list_right").append(str_2);
                            $(k).find(".crowd_status").css('color','#999');
                            $(k).find(".crowd_jd").remove();
                        }
                    } 
                }else{
                    $('.crowd_list').append('<li class="crowd_box_bg">暂无众筹项目</li>')
                }
            }else{
                tc(e.message);
            }
        }
    });
    // 已完成的众筹项目
    $.ajax({
        type:'post',
        url:url_1,
        dataType:'json',
        success:function(respone){
            if(respone.code == '0'){
                $('.crowd_finish_bg').css('display','none');
                var finishCrowdTpl = $("#finishCrowdTpl").html(); 
                if(respone.data.length != '0'){
                    for(var i=0;i<respone.data.length;i++){
                        var finishCrowdListCont=template( 
                            finishCrowdTpl, { 
                                finishCrowdList: [{ 
                                    itemCommonId:respone.data[i].itemCommonId,           //众筹项目Id
                                    itemName:respone.data[i].itemName,                   //众筹项目名称
                                    itemSummary:respone.data[i].itemSummary,             //众筹项目简介
                                    exhibitionImage:respone.data[i].exhibitionImage,     //众筹项目封面图片
                                }] 
                            } 
                        ); 
                        $('.finish_crowd').append(finishCrowdListCont);
                    }
                }else{
                    $('.finish_crowd').append('<div style="width:100%;height:200px;line-height:200px;text-align:center;">暂无已完成的众筹项目</div>')
                }
            }else{
                tc(e.message); 
            }
        }
    })
// 小数转化成百分数
function toPercent(point){
    var str=Number(point*100).toFixed(1);
    str+="%";
    return str;
}
function getLocalTime(nS) {     
    return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");  
}
// 时间格式化
function add0(m){return m<10?'0'+m:m }  
function formatDate(needTime){  
    //needTime是整数，否则要parseInt转换  
    var time = new Date(parseInt(needTime)*1000);  
    var y = time.getFullYear();  
    var m = time.getMonth()+1;  
    var d = time.getDate();  
    var h = time.getHours();  
    var mm = time.getMinutes();  
    var s = time.getSeconds();  
    return y+'/'+add0(m)+'/'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);  
} 
// 将 < > 等符号转化成 < > 等标签符号
function html_decode(str){  
    var s = "";  
    if (str.length == 0) return "";  
    s = str.replace(/</g, '<');  
    s = s.replace(/"/g, '"');  
    s = s.replace(/>/g, ">");  
    s = s.replace(/ /g, " ");  
    s = s.replace(/'/g, "\'");  
    s = s.replace(/"/g, "\"");  
    return s;  
}
//到某一个时间的倒计时
//getEndTime('2017/7/22 16:0:0')
//result："剩余时间6天 2小时 28 分钟20 秒"
function getEndTime(endTime) {
    var startDate = new Date(); //开始时间，当前时间
    var endDate = new Date(endTime); //结束时间，需传入时间参数
    var t = endDate.getTime() - startDate.getTime(); //时间差的毫秒数
    var d = 0,
        h = 0,
        m = 0,
        s = 0;
    if (t >= 0) {
        d = Math.floor(t / 1000 / 3600 / 24);
        h = Math.floor(t / 1000 / 60 / 60 % 24);
        m = Math.floor(t / 1000 / 60 % 60);
        s = Math.floor(t / 1000 % 60);
        (m<10) ? m='0'+m : m= m;
        (s<10) ? s='0'+s : s= s;
    }
    if(d<=0){
        if(h<=0){
            return `${m}分钟${s}秒`;
        }else{
            return `${h}小时${m}分钟`;
        }
    }else{
        return `${d}天${h}小时`;
    } 
}
})